/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package program.utilities;

import java.io.*;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import program.beans.Bill;
import program.beans.BillAmount;
import program.beans.Category;
import program.properties.PropertyManager;

/**
 * Utility-class which writes a turnover tax advance to a pdf-file
 *
 * Responsible: Bernhard
 */
public class TurnOverTaxUtilities {

    private BigDecimal turnOverTax = new BigDecimal(0); // Umsatzsteuer
    private BigDecimal inputTax = new BigDecimal(0); //Vorsteuer

    /**
     * Generates a turnover tax advance out of delivered bills and writes it to
     * a pdf-file with assistance of the PDFUtilities-Class
     *
     * @param bills
     * @param cats
     * @param numKFZ
     * @param amountKFZ
     * @param from
     * @param since
     * @return turnover tax advance pdf-file
     */
    public File createTurnOverTaxAdvanceReturn(ArrayList<Bill> bills, ArrayList<Category> cats, int numKFZ,
            BigDecimal amountKFZ, Date from, Date since) throws Exception {
        File f = null;
        String uid = PropertyManager.getInstance().getValue("uid");
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        String fileContent = getTemplate();
        fileContent = fileContent.replaceAll("#earning", createInOutTable(bills, cats, 1));
        fileContent = fileContent.replaceAll("#outgoing", createInOutTable(bills, cats, 0));
        fileContent = fileContent.replaceAll("#uid", uid);
        fileContent = fileContent.replaceAll("#ust", NumberFormat.getCurrencyInstance().format(turnOverTax));
        fileContent = fileContent.replaceAll("#vst", NumberFormat.getCurrencyInstance().format(inputTax));
        fileContent = fileContent.replaceAll("#diff", NumberFormat.getCurrencyInstance().format(turnOverTax.subtract(inputTax)));
        fileContent = fileContent.replaceAll("#numKFZ", numKFZ + "");
        fileContent = fileContent.replaceAll("#sumKFZ", NumberFormat.getCurrencyInstance().format(amountKFZ.multiply(new BigDecimal(numKFZ))));
        fileContent = fileContent.replaceAll("€", "&euro;");

        f = File.createTempFile("totu", ".html");
        FileWriter fw = new FileWriter(f);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(fileContent);
        bw.newLine();
        bw.close();

        return PDFUtilities.createPDF(f);


    }

    private String getTemplate() throws Exception {
        String template = "";
        File f = new File("templates\\turnovertaxreturn.html");
        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);

        while (br.ready()) {
            template += br.readLine();
        }

        br.close();

        return template;
    }

    /**
     * Creates a html-table out of delivered bills, delivered categories and a
     * delivered type
     *
     * @param bills
     * @param cats
     * @param type
     * @return html-table
     */
    private String createInOutTable(ArrayList<Bill> bills, ArrayList<Category> cats, int type) {
        String table = "";
        String title = (type == 0) ? "Ausgaben" : "Einnahmen";
        String tax = (type == 0) ? "Vorsteuer" : "Umsatzsteuer";
        HashMap<BigDecimal, BigDecimal> taxSumBrutto = new HashMap<>();
        HashMap<BigDecimal, BigDecimal> taxSumNetto = new HashMap<>();

        table += "<p class=\"auto-style7\"><strong><em>" + title + "</em></strong></p>";
        table += "<table cellspacing=\"0\" class=\"auto-style3\" style=\"width: 100%\">";
        table += "<tr>";
        table += "<td class=\"auto-style5\"><em><strong>Datum</strong></em></td>";
        table += "<td class=\"auto-style5\"><em><strong>Firma</strong></em></td>";
        table += "<td class=\"auto-style5\"><em><strong>Bruttobetrag</strong></em></td>";
        table += "<td class=\"auto-style5\"><em><strong>Prozentsatz</strong></em></td>";
        table += "<td class=\"auto-style5\"><em><strong>Nettobetrag</strong></em></td>";
        table += "</tr>";

        for (Category c : cats) {
            int count = 0;
            String help = "";
            help += "<tr>";
            help += "<td class=\"auto-style6\" colspan=\"5\"><strong>Kategorie:" + c.getDescription() + "</strong></td>";
            help += "</tr>";

            for (Bill b : bills) {

                if (b.getCat().equals(c) && b.getType() == type) {
                    count++;
                    for (BillAmount ba : b.getAmounts()) {
                        if (ba.getPercentage().doubleValue() > 0) {
                            help += "<tr>";
                            help += "<td class=\"auto-style4\">" + b.getBillDate() + "</td>";
                            help += "<td class=\"auto-style4\">" + b.getComp().getName() + "</td>";
                            help += "<td class=\"auto-style4\">" + ba.getBruttoFormated() + "</td>";
                            help += "<td class=\"auto-style4\">" + ba.getPercentage().floatValue() + " %</td>";
                            help += "<td class=\"auto-style4\">" + ba.getNettoFormated() + "</td>";
                            help += "</tr>";
                            if (taxSumBrutto.containsKey(ba.getPercentage())) {
                                BigDecimal sum = taxSumBrutto.get(ba.getPercentage());
                                sum = sum.add(ba.getBrutto());
                                taxSumBrutto.remove(ba.getPercentage());
                                taxSumBrutto.put(ba.getPercentage(), sum);

                                BigDecimal sumNetto = taxSumNetto.get(ba.getPercentage());
                                sumNetto = sumNetto.add(ba.getNetto());
                                taxSumNetto.remove(ba.getPercentage());
                                taxSumNetto.put(ba.getPercentage(), sumNetto);
                            } else {
                                BigDecimal sum = new BigDecimal(0);
                                sum = sum.add(ba.getBrutto());
                                taxSumBrutto.put(ba.getPercentage(), sum);

                                BigDecimal sumNetto = new BigDecimal(0);
                                sumNetto = sumNetto.add(ba.getNetto());
                                taxSumNetto.put(ba.getPercentage(), sumNetto);
                            }
                        }
                    }
                }
            }

            if (count > 0) {
                table += help;
            }

            help = "";
        }
        table += "</table><br><br><br>";

        table += "<table cellspacing=\"0\" class=\"auto-style3\" style=\"width: 49%\">";
        table += " <tr>";
        table += "<td class=\"auto-style5\" style=\"width: 168px\"><strong><em>Prozentsatz</em></strong></td>";
        table += "<td class=\"auto-style5\"><strong><em>Summe Bruttobetrag</em></strong></td>";
        table += "<td class=\"auto-style5\"><strong><em>Summe Nettobetrag</em></strong></td>";
        table += "<td class=\"auto-style5\"><strong><em>Differenz</em></strong></td>";
        table += "</tr>";

        BigDecimal sumDiff = new BigDecimal(0);
        for (BigDecimal key : taxSumBrutto.keySet()) {
            BigDecimal sumBrutto = taxSumBrutto.get(key);
            BigDecimal sumNetto = taxSumNetto.get(key);
            table += "<tr>";
            table += "<td class=\"auto-style4\" style=\"width: 168px\">" + key + "</td>";
            table += "<td class=\"auto-style4\">" + NumberFormat.getCurrencyInstance().format(sumBrutto) + "</td>";
            table += "<td class=\"auto-style4\">" + NumberFormat.getCurrencyInstance().format(sumNetto) + "</td>";
            table += "<td class=\"auto-style4\">" + NumberFormat.getCurrencyInstance().format(sumBrutto.subtract(sumNetto)) + "</td>";
            table += "</tr>";
            sumDiff = sumDiff.add(sumBrutto.subtract(sumNetto));
        }
        table += "<tr>";
        table += "<td class=\"auto-style8\" colspan=\"3\"><strong><em>" + tax + "</em></strong></td>";
        table += "<td class=\"auto-style8\">" + NumberFormat.getCurrencyInstance().format(sumDiff) + "</td>";
        table += "</tr>";

        table += "</table>";
        if (type == 0) {
            inputTax = sumDiff;
        } else {
            turnOverTax = sumDiff;
        }

        return table;
    }
}
